import DAL.SqlHelper;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.xml.transform.Result;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String uname = request.getParameter("username");
        String pwd = request.getParameter("password");

//        Connection con;
//        String driver="com.mysql.jdbc.Driver";
//        //这里我的数据库是user
//        String url="jdbc:mysql://localhost:3306/user";
//        String user="root";
//        String password="123456";
//        try {
//            Class.forName(driver);
//            con = DriverManager.getConnection(url, user, password);
//            if (!con.isClosed()) {
//                System.out.println("数据库连接成功");
//            }
//            Statement statement = con.createStatement();
//            String sql = "select * from users where username='"+uname+"' and password='"+pwd+"'";
//            ResultSet resultSet = statement.executeQuery(sql);
            String sql = "select * from users where username='"+uname+"' and password='"+pwd+"'";
            ResultSet resultSet = SqlHelper.executeQuery(sql);
//        try {
        try {
            if (resultSet.next()) {

                System.out.println("表单输入数据：" + uname + " " + pwd);
                System.out.println("用户名：" + uname + " 密码:" + pwd);
                System.out.println("登录成功，欢迎你 "+uname);
                out.println("登录成功" + "<br>" + "您的用户名：" + uname + "<br>" + "您的密码：" + pwd + "<br>");

                Cookie cookieUser = new Cookie("username",uname);
                cookieUser.setMaxAge(60*60*24*30);
                response.addCookie(cookieUser);

                Cookie cookiePwd = new Cookie("password",pwd);
                cookiePwd.setMaxAge(60*60*24*30);
                response.addCookie(cookiePwd);

                HttpSession session = request.getSession();
                session.setMaxInactiveInterval(10);
                ServletContext servletContext = getServletContext();
                Object num = servletContext.getAttribute("number");
//                out.println("当前在线人数：" + num);
                request.setAttribute("username",uname);
                request.setAttribute("password",pwd);
                request.getRequestDispatcher("index.jsp").forward(request,response);
            }
            else{
                System.out.println("表单输入数据：" + uname + " " + pwd);
                System.out.println("用户名或者密码错误");
                out.println("登录失败" + "<br>" + "用户名或者密码错误");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

//        resultSet.close();
//            con.close();
//        } catch (ClassNotFoundException e) {
//            System.out.println("数据库驱动没有安装");
//
//        } catch (SQLException e) {
//            System.out.println("数据库连接失败");
//        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}